package com.example.myone.Adapter;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myone.Entity.Article;
import com.example.myone.Entity.Music;
import com.example.myone.R;
import com.example.myone.Util.ImageUtils;

import java.util.List;
import java.util.Objects;

public class RadioArticleAdapter extends RecyclerView.Adapter<RadioArticleAdapter.RadioArticleViewHolder> {
    public List<Article> articleList;
    public Context context;
    private OnItemClickListener listener;

    public RadioArticleAdapter(Context context, List<Article> articleList, OnItemClickListener listener) {
        this.context = context;
        this.articleList = articleList;
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(Article article);
    }
    @SuppressLint("NotifyDataSetChanged")
    public void updateData(List<Article> list) {
        articleList.clear();
        articleList.addAll(list);
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public RadioArticleAdapter.RadioArticleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.radio_pager_music_item, parent, false);
        return new RadioArticleAdapter.RadioArticleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RadioArticleAdapter.RadioArticleViewHolder holder, int position) {
        Article article = articleList.get(position);
        ImageUtils.setImageByBase64Str(context, holder.cover, article.cover);
        holder.title.setText(article.title);
        holder.playerButton.setOnClickListener(v -> {
            listener.onItemClick(article);
        });

    }

    @Override
    public int getItemCount() {
        return articleList.size();
    }

    public static class RadioArticleViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        ImageView cover, playerButton;

        public RadioArticleViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            cover = itemView.findViewById(R.id.cover);
            playerButton = itemView.findViewById(R.id.player_button);
        }
    }
}
